﻿namespace Socut
{
    using System;
    using System.Text;
    using System.Configuration;
    using System.Security.Cryptography;
    using System.IO;

    public class CLink
    {
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

        public static readonly string Source = DecryptDES(ConfigurationManager.AppSettings["FCKeditor:DBLnk"], "p@ssw0rd");

        public static short Type()
        {
            return Type(0);
        }

        private static string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }

        public static short Type(int ConnIndex)
        {
            string source = Source;
            if (source.IndexOf("|") > -1)
            {
                source = source.Split(new char[] { '|' })[ConnIndex];
            }
            if (source.Replace(" ", "").ToUpper().IndexOf(";SERVER=") > -1)
            {
                return 1;
            }
            return 0;
        }
    }
}

